package com.company;

public class race {

        public static void main(String arg[]) {
            Animal a1,a2;
            a1 = new Animal("兔子",5);
            a2 = new Animal("乌龟",1);
            a2.setPriority(Thread.MAX_PRIORITY);//让乌龟的运行优先级更高
            a1.setName("兔子");
            a2.setName("乌龟");
            a1.start();
            a2.start();
        }

    }
    class Animal extends Thread{
        int speed;//速度
        public Animal(String str,int speed){
            this.speed=speed;
        }
        public void run(){
            int distance = 0;
            int sleepTime;
            while(distance<=1000){
                System.out.println(getName()+"  "+distance);
                try{
                    distance+=speed;//每次跑的距离简单用速度计算
                    sleepTime = (int)(speed+Math.random()*speed);
                    //速度快休息时间更长
                    sleep(sleepTime);
                }catch(InterruptedException e){}
            }
        }
    }

